package com.yiwei.ydd.activity;

import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.adapter.CouponSelectAdapter;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.CouponListBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.event.SelectCouponEvent;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class CouponSelectActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.img_status)
    ImageView imgStatus;
    @BindView(R.id.layout_check)
    FrameLayout layoutCheck;
    @BindView(R.id.layout_has_data)
    LinearLayout layoutHasData;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    private CouponSelectAdapter adapter;
    private Unbinder unbiner;
    private String limit;
    private boolean isOkRefresh = false;
    private boolean isNoData = false;
    private int currentPage = 1;
//    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
//        @Override
//        public void onLoadMore(int page) {
//            getCouponList(page);
//        }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_select);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        limit = getIntent().getStringExtra("limit");
        txtTitle.setText("加油包");
        adapter = new CouponSelectAdapter(this, 0);
        adapter.setOnChooseListener(new CouponSelectAdapter.OnChooseListener() {
            @Override
            public void onClick(String money, String id) {

                EventBus.getDefault().post(new SelectCouponEvent(id, money));
                txtTitle.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 150);
            }
        });
//        list.addOnScrollListener(onLoadeMoreListener);
        list.setAdapter(adapter);

        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY == (scrollView.getChildAt(0).getMeasuredHeight() - scrollView.getMeasuredHeight())) {
                    //底部加载
                    if (isOkRefresh && !isNoData) {
                        isOkRefresh = false;
                        currentPage++;
                        getCouponList(currentPage);

                    }
                }
                if (refresh != null) {
                    refresh.setEnabled(scrollView.getScrollY() == 0);
                }
            }
        });

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
//                onLoadeMoreListener.setPage();
                getCouponList(1);
            }
        });
        getCouponList(1);
    }

    public void getCouponList(int page) {
//        "可使用", "已使用", "已过期"
        //状态（0：过期，1:有效，2：已使用)
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);
        CouponListBean bean = new CouponListBean();
        bean.page = page;
        bean.status = "1";
        bean.use_limit = limit;
        Api.api_service.getCouponList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                        isOkRefresh = true;
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {

                    if (result.datas != null && result.datas.size() != 0) {
                        adapter.add(result.datas, page == 1);
                        isNoData = false;
                    }else {
                        isNoData = true;
                    }
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                        layoutHasData.setVisibility(View.GONE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                        layoutHasData.setVisibility(View.VISIBLE);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }


    @OnClick({R.id.btn_back, R.id.layout_check})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.layout_check:
                EventBus.getDefault().post(new SelectCouponEvent("", "0"));
                imgStatus.setImageResource(R.mipmap.img_coupon_check);
                txtTitle.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 150);
                break;
        }
    }
}
